let mv = new Vue({
    el: '#tb',
    data: {
        list: list(),
        product:{
            name:'',
            price:''
        }
    },
    computed: {
        countPrices: function () {
            let allPrice = 0;
            this.list.forEach(element => {
                allPrice += element.price * element.count
            });
            return allPrice;
        },
        discounts: function () {
            return (this.countPrices * 0.8).toFixed(2);
        }
    },
    methods: {
        btnMinus: function (index) {
            if (this.list[index].count>1) {
                this.list[index].count -= 1
            }
        },
        btnAdd: function (index) {
            this.list[index].count += 1
        },
        productAdd: function () {
            console.log(parseFloat(this.product.price));

            if (this.product.name!=='' && this.product.price!=='' && parseFloat(this.product.price)!==NaN) {
                let obj={
                    id:parseInt(this.list[this.list.length-1].id+1),
                    name:this.product.name,
                    count:1,
                    price:parseFloat(this.product.price).toFixed(2),
                }
                this.list.push(obj)
                this.product.name=''
                this.product.price=''
            }else if(parseFloat(this.product.price)===NaN){
                alert('请填写正确的价格!')
            }else{
                alert('所填信息不能为空！')
            }
        },
        btnDel: function (index) {
            if ( confirm('确认删除')) {
                this.list.splice(index, 1)
            }
        }
    }
})


function list(){
    return [
        {
            id: 1,
            name: 'Xiaomi 11 Ultra',
            count: 1,
            price: 3999.00.toFixed(2),
        },
        {
            id: 2,
            name: 'iPhone 13',
            count: 1,
            price: 6799.00.toFixed(2)
        },
        {
            id: 3,
            name: 'Xiaomi MIX 4',
            count: 1,
            price: 4199.00.toFixed(2)
        },
        {
            id: 4,
            name: '黑鲨5 Pro',
            count: 1,
            price: 4199.00.toFixed(2)
        },
    ];
}